﻿using Common.AspNetCore;
using Common.EF;
using Common.Infrastructure;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Test.Data.Entity;

namespace Test.Api.Controllers
{ 
    [Route("api/WeatherForecast")]
    public class WeatherForecastController : ApiController
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
         
        private readonly ILogger<WeatherForecastController> logger;
        private readonly ICommonRepository repository;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IServiceProvider provider, ICommonRepository common)
            : base(provider)
        {
            this.logger = logger;
            this.repository = common;
        }

        [HttpGet]
        public ApiActionResult<ModelSaveRequest> Get()
        {
            return SafeExecute<ModelSaveRequest>((param) =>
            {
                ModelSaveRequest request = new ModelSaveRequest(); 
                var tt = new t_test();
                var ids = this.repository.GetEntityIds(new IDParam() { Count = 1, EntityName = "t_test" });
                tt.id = ids[0];
                tt.name = "1";
                request.Details.Add(new ModelSaveDetailRequest() { ModelData = tt.ToJson(), ModelName = "t_test" });

                var tt2 = new t_test_detail();
                var ids1 = this.repository.GetEntityIds(new IDParam() { Count = 1, EntityName = "t_test_detail" });
                tt2.id = ids1[0];
                tt2.test_id = tt.id;
                request.Details.Add(new ModelSaveDetailRequest() { ModelData = tt.ToJson(), ModelName = "t_test_detail" });

                return request;
            });
        }

        [NonAction]
        [CapSubscribe("test_test")]
        public void PdtOrderEdit(string p)
        {
            this.logger.LogInformation(p);
        }

        public async Task GetTestAsync()
        {
            await Task.Run(() =>
            { 
                if (1 > 0)
                {
                    throw new Exception("sxxxxx");
                }
            });
        }
    }
}
